package com.example.sbeducation.controller;

import com.example.sbeducation.common.Result;
import com.example.sbeducation.exception.BusinessException;
import com.example.sbeducation.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/file")
public class FileController {

  @Autowired
  private FileUtil fileUtil;

  /**
   * 上传作业附件
   */
  @PostMapping("/homework")
  public Result<String> uploadHomeworkFile(@RequestParam("file") MultipartFile file) {
    log.info("接收到作业附件上传请求，文件大小: {}", file != null ? file.getSize() : "文件为空");
    try {
      String filePath = fileUtil.uploadFile(file, "homework");
      log.info("作业附件上传成功: {}", filePath);
      return Result.success(filePath);
    } catch (BusinessException e) {
      log.error("作业附件上传业务异常: {}", e.getMessage());
      return Result.error(e.getCode(), e.getMessage());
    } catch (Exception e) {
      log.error("作业附件上传系统异常", e);
      return Result.error(500, "文件上传失败: " + e.getMessage());
    }
  }

  /**
   * 上传作业提交附件
   */
  @PostMapping("/submission")
  public Result<String> uploadSubmissionFile(@RequestParam("file") MultipartFile file) {
    log.info("接收到作业提交附件上传请求，文件大小: {}", file != null ? file.getSize() : "文件为空");
    try {
      String filePath = fileUtil.uploadFile(file, "submission");
      log.info("作业提交附件上传成功: {}", filePath);
      return Result.success(filePath);
    } catch (BusinessException e) {
      log.error("作业提交附件上传业务异常: {}", e.getMessage());
      return Result.error(e.getCode(), e.getMessage());
    } catch (Exception e) {
      log.error("作业提交附件上传系统异常", e);
      return Result.error(500, "文件上传失败: " + e.getMessage());
    }
  }

  /**
   * 上传聊天图片
   */
  @PostMapping("/chat")
  public Result<String> uploadChatImage(@RequestParam("file") MultipartFile file) {
    log.info("接收到聊天图片上传请求，文件大小: {}", file != null ? file.getSize() : "文件为空");
    try {
      String filePath = fileUtil.uploadFile(file, "chat");
      return Result.success(filePath);
    } catch (BusinessException e) {
      log.error("聊天图片上传业务异常: {}", e.getMessage());
      return Result.error(e.getCode(), e.getMessage());
    } catch (Exception e) {
      log.error("聊天图片上传系统异常", e);
      return Result.error(500, "文件上传失败: " + e.getMessage());
    }
  }

  /**
   * 上传用户头像
   */
  @PostMapping("/avatar")
  public Result<String> uploadAvatar(@RequestParam("file") MultipartFile file) {
    log.info("接收到用户头像上传请求，文件大小: {}", file != null ? file.getSize() : "文件为空");
    try {
      String filePath = fileUtil.uploadFile(file, "avatar");
      return Result.success(filePath);
    } catch (BusinessException e) {
      log.error("用户头像上传业务异常: {}", e.getMessage());
      return Result.error(e.getCode(), e.getMessage());
    } catch (Exception e) {
      log.error("用户头像上传系统异常", e);
      return Result.error(500, "文件上传失败: " + e.getMessage());
    }
  }

  /**
   * 删除文件
   */
  @DeleteMapping("")
  public Result<Boolean> deleteFile(@RequestParam("filePath") String filePath) {
    log.info("接收到文件删除请求: {}", filePath);
    try {
      boolean success = fileUtil.deleteFile(filePath);
      return Result.success(success);
    } catch (Exception e) {
      log.error("文件删除异常", e);
      return Result.error(500, "文件删除失败: " + e.getMessage());
    }
  }
}